package javabase.schoolwork.day6_14;

import org.junit.Test;

import java.io.*;

/**
 * @author fance
 * @date 2018/6/14 20:56
 */
public class Sum {

    public void fun1() {
        File file = new File("src/javabase/schoolwork/day6_14/test1.txt");
        File dircur = new File("src/javabase/schoolwork/day6_14");
        File dir = new File("src/javabase/schoolwork/day6_14/testdir");
        dir.mkdir();
        System.out.println(file.isDirectory());
        System.out.println(dir.isDirectory());
        File[] files = dircur.listFiles();
        for (File  file1 : files) {
            System.out.print(file1.getName() + " ");
        }
        dir.delete();
    }
    public void fun2(File file) {
        if (file != null) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        fun2(files[i]);
                    }
                }
            } else {
                System.out.println(file);
            }
        }
    }

    public static String fun3(String fileName) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        StringBuilder stringBuilder = new StringBuilder();
        String s;
        while ((s = reader.readLine()) != null) {
            stringBuilder.append(s + "\n");
        }
        return stringBuilder.toString();
    }

    public void randomAccessFileTest() throws IOException {
        File file = new File("src/javabase/schoolwork/day6_14/test.txt");
        RandomAccessFile accessFile = new RandomAccessFile(file,"rw");
        accessFile.writeChars("zhangsan,30");
        accessFile.writeChars("lisi,31");
        accessFile.writeChars("wangwu,32");
        /*byte[] b = new byte[20];
        int offset = 0;
        int limit = 12;
        while (accessFile.read(b,offset,limit) != -1) {
            System.out.println(b);
            offset = limit + 1;
            limit = 32;
        }*/
    }


    public void objectOutTest() throws IOException, ClassNotFoundException {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new FileOutputStream("src/javabase/schoolwork/day6_14/test.txt")
        );
        Person person = new Person("fance",3,"haha");
        objectOutputStream.writeObject(person);
        objectOutputStream.flush();
        objectOutputStream.close();

        ObjectInputStream objectInputStream = new ObjectInputStream(
                new FileInputStream("src/javabase/schoolwork/day6_14/test.txt")
        );
        Person fance = (Person) objectInputStream.readObject();
        System.out.println(fance);
        objectInputStream.close();
    }

    public void dataOutTest() throws IOException {
        DataOutputStream dataOutputStream = null;
        try {
            dataOutputStream = new DataOutputStream(
                    new FileOutputStream("src/javabase/schoolwork/day6_14/test.txt")
            );

            dataOutputStream.writeUTF("ab中国");
            dataOutputStream.writeBoolean(false);
            dataOutputStream.writeLong(100L);
            System.out.println("ok");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dataOutputStream != null) {
                dataOutputStream.close();
            }
        }
    }

   public void fileOutTest() throws FileNotFoundException {
        FileOutputStream fileOutputStream = null;
        fileOutputStream = new FileOutputStream(new File("src/javabase/schoolwork/day6_14/test.txt"));

        PrintStream printStream = new PrintStream(fileOutputStream,true);
        if (printStream != null) {
            System.setOut(printStream);
        }
       for (int i = 0; i <= 255; i++) {
           System.out.print((char)i);
           if (i % 50 == 0) {
               System.out.println();
           }
       }
    }

   public boolean copyFile(String src,String des)   {
       try {
           FileOutputStream fileOutputStream = new FileOutputStream(des);
           FileInputStream fileInputStream = new FileInputStream(src);
           byte[] bytes = new byte[50];
           while (fileInputStream.read(bytes, 0, 50) != -1) {
               fileOutputStream.write(bytes, 0, 50);
           }
       } catch (IOException e) {
           e.printStackTrace();
           return false;
       }
       return true;
   }

   public void getString() throws IOException {

   }
   @Test
    public void test() throws IOException, ClassNotFoundException {
//        randomAccessFileTest();
//        fun1();
//        fun2(new File("D:\\codefuninwindows\\src\\javabase\\thinkinjava"));
//        fun3("src/javabase/schoolwork/day6_14/test1.txt");

//        objectOutTest();
//        dataOutTest();
//        fileOutTest();
//        copyFile("src/javabase/schoolwork/day6_14/test.txt","src/javabase/schoolwork/day6_14/test1.txt");

       getString();
    }

    public static void main(String[] args) throws IOException {
       /* StringReader in = new StringReader(fun3("src/javabase/schoolwork/day6_14/Sum.java"));
        int c;
        while ((c = in.read()) != -1) {
            System.out.print(c);
        }*/

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//        OutputStream writer = new BufferedWriter(new OutputStreamWriter(System.out));
        while (true) {
            String s = reader.readLine();
            if (s.equals("e") || s.equals("exit")) {
                break;
            }

            System.out.println(s.toUpperCase());
        }
    }

}
